package hdelrs

import (
	"car-master/cache"
	"car-master/models"
	"car-master/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	name := c.PostForm("name")
	passes := c.PostForm("passes")
	if name == "" || passes == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "redis_login" + name
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if po >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次，禁止登录",
		})
		return
	}
	var user models.User
	models.DB.Where("name=?", name).First(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Passes != pkg.Md5(passes) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if po == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误超过3次需要限制登录2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func Role(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.NodeOrder{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func NodeOrder(c *gin.Context) {
	uid := c.Query("uid")
	/*
			SELECT
		  *
		FROM
		  roles
		  LEFT JOIN role_orders ON role_orders.uid = roles.id
		  WHERE
		  role_orders.uid=1
	*/
	var role []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN role_orders ON role_orders.rid = roles.id").
		Where("role_orders.uid=?", uid).
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
}
func Po(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN node_orders ON node_orders.rid = nodes.id
		  WHERE
		  node_orders.rid=1
	*/
	var node []models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN node_orders ON node_orders.nid = nodes.id").
		Where("node_orders.rid=?", rid).
		Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
}
func Car(c *gin.Context) {
	CarCode := c.PostForm("car_code")
	CarName := c.PostForm("car_name")
	CarNum := c.PostForm("car_num")

	if CarCode == "" || CarName == "" || CarNum == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Car{
		CarCode: CarCode,
		CarName: CarName,
		CarNum:  CarNum,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Order(c *gin.Context) {
	OrderNo := c.PostForm("order_no")
	FromAddress := c.PostForm("from_address")
	ToAddress := c.PostForm("to_address")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))

	if OrderNo == "" || FromAddress == "" || ToAddress == "" || Cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderNo:     OrderNo,
		FromAddress: FromAddress,
		ToAddress:   ToAddress,
		Cid:         Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_num as CarNum,
		  orders.order_no as OrderNo,
		  orders.from_address as FromAddress,
		  orders.to_address as ToAddress
		FROM
		  cars
		  LEFT JOIN orders ON orders.cid = cars.id
	*/
	var list []models.OrderList

	redis_key := "redis_list" + strconv.Itoa(page)
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("cars").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_num as CarNum",
				"orders.order_no as OrderNo",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress",
			).Joins("LEFT JOIN orders ON orders.cid = cars.id").
			Offset(offset).
			Limit(size).
			Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	//models.DB.Table("cars").
	//	Select(
	//		"orders.id as OrderId",
	//		"cars.car_code as CarCode",
	//		"cars.car_name as CarName",
	//		"cars.car_num as CarNum",
	//		"orders.order_no as OrderNo",
	//		"orders.from_address as FromAddress",
	//		"orders.to_address as ToAddress",
	//	).Joins("LEFT JOIN orders ON orders.cid = cars.id").
	//	Offset(offset).
	//	Limit(size).
	//	Find(&list)

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return
}
func OrderLists(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_num as CarNum,
		  orders.order_no as OrderNo,
		  orders.from_address as FromAddress,
		  orders.to_address as ToAddress
		FROM
		  cars
		  LEFT JOIN orders ON orders.cid = cars.id
	*/
	var list models.OrderList

	redis_key := "redis_xq" + id
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("cars").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_num as CarNum",
				"orders.order_no as OrderNo",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress",
			).Joins("LEFT JOIN orders ON orders.cid = cars.id").
			Where("orders.id=?", id).
			Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func CarRole(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := models.NodeOrder{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func UserRole(c *gin.Context) {
	uid := c.Query("uid")
	/*
			SELECT
		  *
		FROM
		  roles
		  LEFT JOIN role_orders ON role_orders.rid = roles.id
		  WHERE
		role_orders.uid=1
	*/
	var roles []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN role_orders ON role_orders.rid = roles.id").
		Where("role_orders.uid=?", uid).Find(&roles)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": roles,
	})
	return
}
func UserNode(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN node_orders ON node_orders.nid = nodes.id
		  WHERE
		  node_orders.rid=1
	*/
	var node []models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN node_orders ON node_orders.nid = nodes.id").
		Where("node_orders.rid=?", rid).Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}
func CarAdd(c *gin.Context) {
	CarCode := c.PostForm("car_code")
	CarName := c.PostForm("car_name")
	CarNum := c.PostForm("car_num")

	if CarCode == "" || CarName == "" || CarNum == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Car{
		CarCode: CarCode,
		CarName: CarName,
		CarNum:  CarNum,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	OrderNo := c.PostForm("order_no")
	FromAddress := c.PostForm("from_address")
	ToAddress := c.PostForm("to_address")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))

	if OrderNo == "" || FromAddress == "" || ToAddress == "" || Cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderNo:     OrderNo,
		FromAddress: FromAddress,
		ToAddress:   ToAddress,
		Cid:         Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderLisTes(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_num as CarNum,
		  orders.order_no as OrderNo,
		  orders.from_address as FromAddress,
		  orders.to_address as ToAddress
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.cid
	*/
	var list []models.OrderList

	redid_key := "redis_list" + strconv.Itoa(page)
	po, _ := cache.Rdb.Get(cache.Ctx, redid_key).Result()
	if po == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_num as CarNum",
				"orders.order_no as OrderNo",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress").
			Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Offset(offset).Limit(size).Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redid_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return
}
func OrderXq(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_num as CarNum,
		  orders.order_no as OrderNo,
		  orders.from_address as FromAddress,
		  orders.to_address as ToAddress
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.cid
	*/
	var list models.OrderList

	redid_key := "redis_id" + id
	po, _ := cache.Rdb.Get(cache.Ctx, redid_key).Result()
	if po == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_num as CarNum",
				"orders.order_no as OrderNo",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress").
			Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Where("orders.id=?", id).Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redid_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
